﻿#include "ExampleLayer.h"

#include "GameEngine.h"
#include "core/log/LogSystem.h"
#include "../../GameEngine/ThirdParty/imgui/imgui.h"
#include "function/RenderSystem/AbstractBuffer.h"
#include "function/RenderSystem/AbstractRenderer.h"
#include "function/RenderSystem/AbstractShader.h"
#include "function/RenderSystem/Texture.h"
#include "function/RenderSystem/OpenGL/OpenGLShader.h"
#include "glm/gtc/type_ptr.hpp"

namespace Editor
{
	ExampleLayer::ExampleLayer() : Layer("ExampleLayer"), cameraController(1.0f)
	{
		vertexArray.reset(Engine::VertexArray::Create());

		// constexpr float vertices[20] = {
		// 	 0.5f,  0.5f, 0.0f,		1.0f, 1.0f,
		// 	 0.5f, -0.5f, 0.0f,		1.0f, 0.0f,
		// 	-0.5f, -0.5f, 0.0f,		0.0f, 0.0f,
		// 	-0.5f,  0.5f, 0.0f,		0.0f, 1.0f
		// };
		constexpr float vertices[20] = {
			-0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
			0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
			0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
			-0.5f, 0.5f, 0.0f, 0.0f, 1.0f
		};
		Engine::Ref<Engine::VertexBuffer> vertexBuffer;
		vertexBuffer.reset(Engine::VertexBuffer::Create(vertices, sizeof(vertices)));

		const Engine::BufferLayout layout = {
			{Engine::ShaderDateType::Float3, "a_Position"},
			{Engine::ShaderDateType::Float2, "a_TexCoord"}
		};
		vertexBuffer->SetLayout(layout);
		vertexArray->AddVertexBuffer(vertexBuffer);

		constexpr unsigned int indices[6] = {
			0, 1, 3,
			1, 2, 3
		};
		Engine::Ref<Engine::IndexBuffer> indexBuffer;
		indexBuffer.reset(Engine::IndexBuffer::Create(indices, sizeof(indices) / sizeof(unsigned int)));
		vertexArray->SetIndexBuffer(indexBuffer);

		// todo:需要改成相对路径
		shader = shaderLibrary.Load(
			"UnLit", "D:/C++ Projects/mini-game-engine/GameEngine/assets/shader/OpenGL/UnLit.glsl");

		texture = Engine::Texture2D::Create("D:/壁纸/尽灼天幕01.jpg");
		alphaTexture = Engine::Texture2D::Create("C:/Users/Administrator/Downloads/jsongj_com_透明化图片.png");
	}

	void ExampleLayer::OnUpdate(const float deltaTime)
	{
		this->deltaTime = deltaTime;

		cameraController.OnUpdate(deltaTime);

		Engine::AbstractRenderer::BeforeRender({0.45f, 0.55f, 0.60f, 1.00}, cameraController.GetCamera());
		texture->Bind();
		Engine::AbstractRenderer::Submit(vertexArray, shader);
		alphaTexture->Bind();
		Engine::AbstractRenderer::Submit(vertexArray, shader);
		std::dynamic_pointer_cast<Engine::OpenGLShader>(shader)->SetUniformInt("u_Texture", 0);
		Engine::AbstractRenderer::AfterRender();
	}

	void ExampleLayer::OnImGuiRender()
	{
		ImGui::Begin("Information");
		ImGui::Text("DeltaTime:%.3f ms", deltaTime);
		ImGui::ColorEdit3("SquareColor", glm::value_ptr(squareColor));
		ImGui::End();
	}

	void ExampleLayer::OnEvent(Engine::Event& event)
	{
		cameraController.OnEvent(event);

		//Engine::EventDispatcher dispatcher(event);
		// dispatcher.Dispatch<Engine::KeyPressedEvent>(BIND_EVENT_FUNCTION(ExampleLayer::OnKeyPressedEvent));
	}
}
